<template>
    <div>
        <!-- 搜索表单 -->
        <el-form :inline="true" :model="searchFrom" ref="searchFrom" class="demo-form-inline" style="text-align:center">
            <el-form-item label="名称" prop="name">
                <el-input v-model="searchFrom.name" placeholder="名称"></el-input>
            </el-form-item>
            <el-form-item label="地址" >
                <!-- 省市三级联动 -->
                <el-select v-model="searchFrom.provinceId" @change="getCity($event)" filterable placeholder="选择省份" style="width: 130px">
                    <el-option
                            v-for="item in province"
                            :key="item.provinceId"
                            :label="item.provinceName"
                            :value="item.provinceId">
                    </el-option>
                </el-select>
                <el-select v-model="searchFrom.cityId" @change="getArea($event)" filterable placeholder="选择城市" style="width: 130px">
                    <el-option
                            v-for="item in city"
                            :key="item.cityId"
                            :label="item.cityName"
                            :value="item.cityId">
                    </el-option>
                </el-select>
                <el-select v-model="searchFrom.areaId" @change="$forceUpdate()" filterable placeholder="选择区县" style="width: 130px">
                    <el-option
                            v-for="item in area"
                            :key="item.areaId"
                            :label="item.areaName"
                            :value="item.areaId">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="searchBtn()">查询</el-button>
                <el-button type="warning" icon="el-icon-refresh" @click="resetForm()">重置</el-button>
            </el-form-item>
        </el-form>

        <!-- 表格 -->
        <el-table :data="tableData" stripe style="width: 100%">
            <el-table-column prop="id" label="编号"/>
            <el-table-column prop="name" label="门店名称"/>
            <el-table-column label="地址">
                <template slot-scope="scope">
                    <span>{{scope.row.provinceName}}-{{scope.row.cityName}}-{{scope.row.areaName}}</span>
                </template>
            </el-table-column>
            <el-table-column prop="address" label="详细地址"/>
            <el-table-column label="状态">
                <template slot-scope="scope">
                    <span v-if="scope.row.ishot === 1"><el-button size="mini" type="success">常用</el-button></span>
                    <span v-if="scope.row.ishot === 0"><el-button size="mini" type="warning">一般</el-button></span>
                </template>
            </el-table-column>
            <el-table-column prop="addtime" label="创建时间"/>
            <el-table-column align="right">
                <template slot="header">
                    <span style="margin-right:30px">操作</span>
                    <el-button size="mini" type="primary" @click="openAddDialog()">添加</el-button>
                </template>
                <template slot-scope="scope">
                    <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button size="mini" type="danger" @click="handleDelete(scope.$index,scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="block" style="text-align:center">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageNum"
                    :page-sizes="[1, 2, 5, 10]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>

        <!-- 添加弹出框 -->
        <el-dialog title="添加代理门店信息" :visible.sync="dialogAdd">
            <el-form :model="shop" ref="addShop" :rules="rules">
                <el-form-item label="门店名称" label-width="120px" prop="name">
                    <el-input v-model="shop.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="地址" label-width="120px">
                    <!-- 省市三级联动 -->
                    <el-select v-model="shop.provinceId" @change="getCity($event)" filterable placeholder="选择省份">
                        <el-option
                                v-for="item in province"
                                :key="item.provinceId"
                                :label="item.provinceName"
                                :value="item.provinceId">
                        </el-option>
                    </el-select>
                    <el-select v-model="shop.cityId" @change="getArea($event)" filterable placeholder="选择城市">
                        <el-option
                                v-for="item in city"
                                :key="item.cityId"
                                :label="item.cityName"
                                :value="item.cityId">
                        </el-option>
                    </el-select>
                    <el-select v-model="shop.areaId" @change="$forceUpdate()" filterable placeholder="选择区县">
                        <el-option
                                v-for="item in area"
                                :key="item.areaId"
                                :label="item.areaName"
                                :value="item.areaId">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="详细地址" label-width="120px" prop="address">
                    <el-input v-model="shop.address" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="添加时间" label-width="120px" prop="addtime">
                    <el-col :span="11">
                        <el-form-item prop="addtime">
                            <el-date-picker type="date"
                                            format="yyyy-MM-dd"
                                            value-format="yyyy-MM-dd"
                                            placeholder="选择日期" v-model="shop.addtime"
                                            style="width: 100%;"></el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-form-item>
                <el-form-item label="是否为常用门店" label-width="120px" prop="ishot">
                    <el-radio-group v-model="shop.ishot">
                        <el-radio :label="0" border>不常用</el-radio>
                        <el-radio :label="1" aria-checked="true" border>常用</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogAdd=false">取 消</el-button>
                <el-button type="primary" @click="addShopBtn()">确 定</el-button>
            </div>
        </el-dialog>

        <!-- 修改弹出框 -->
        <el-dialog title="添加代理门店信息" :visible.sync="dialogUpdate" >
            <el-form :model="shop" ref="updateShop" :rules="rules">
                <input v-model="shop.id" style="display: none" ></input>
                <el-form-item label="门店名称" label-width="120px" prop="name">
                    <el-input v-model="shop.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="地址" label-width="120px" prop="city">
                    <!-- 省市三级联动 -->
                    <el-select v-model="shop.provinceId" @change="getCity($event)" filterable placeholder="选择省份">
                        <el-option
                                v-for="item in province"
                                :key="item.provinceId"
                                :label="item.provinceName"
                                :value="item.provinceId">
                        </el-option>
                    </el-select>
                    <el-select v-model="shop.cityId" @change="getArea($event)" filterable placeholder="选择城市">
                        <el-option
                                v-for="item in city"
                                :key="item.cityId"
                                :label="item.cityName"
                                :value="item.cityId">
                        </el-option>
                    </el-select>
                    <el-select v-model="shop.areaId" @change="$forceUpdate()" filterable placeholder="选择区县">
                        <el-option
                                v-for="item in area"
                                :key="item.areaId"
                                :label="item.areaName"
                                :value="item.areaId">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="详细地址" label-width="120px" prop="address">
                    <el-input v-model="shop.address" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="添加时间" label-width="120px" prop="addtime">
                    <el-col :span="11">
                        <el-form-item prop="addtime">
                            <el-date-picker type="date"
                                            format="yyyy-MM-dd"
                                            value-format="yyyy-MM-dd"
                                            placeholder="选择日期" v-model="shop.addtime"
                                            style="width: 100%;"></el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-form-item>
                <el-form-item label="是否为常用门店" label-width="120px" prop="ishot">
                    <el-radio-group v-model="shop.ishot">
                        <el-radio :label="0" border>不常用</el-radio>
                        <el-radio :label="1" border>常用</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogUpdate=false">取 消</el-button>
                <el-button type="primary" @click="updateShopBtn()">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                tableData: [],
                total: 0, // 总条数
                pageNum: 1, // 当前页数
                pageSize: 5, // 每页显示几条
                dialogAdd: false,
                dialogUpdate:false,
                tableRowId: '',
                searchFrom: {},
                treeData: [],
                shop: {},  // 添加和编辑的时候的通用代理商信息
                province: [],  // 全国省份数据
                city: [],  // 城市
                area: [], // 区县
                rules: {
                    name: [
                        {required: true, message: '请输入门店名称', trigger: 'blur'},
                    ],
                    address: [
                        {required: true, message: '请输入门店地址', trigger: 'blur'}
                    ],
                    ishot: [
                        {required: true, message: '请输入是否是常用门店', trigger: 'blur'},
                    ],
                    addtime:[
                        {required: true, message: '请输入时间', trigger: 'blur'},
                    ],
                },
            }
        },
        methods: {
            // 重置表格
            resetForm() {
                this.searchFrom={}
                this.getAllShop();
            },
            // 添加按钮
            openAddDialog() {
                // 设置添加的初始值
                this.shop = {ishot:1};
                this.dialogAdd = true;
            },
            // 打开编辑操作
            handleEdit(index, row) {
                // 获取所有的市和区县
                this.$http.get("cs/baseArea/queryCity?id=" + row.provinceId).then(res => {this.city = res.data.t;})
                this.$http.get("cs/baseArea/queryArea?id=" + row.cityId).then(res => { this.area = res.data.t;})
                this.dialogUpdate = true // 打开弹出层
                // 让表单重新赋值
                this.shop = JSON.parse(JSON.stringify(row))

            },
            //每页显示几条改变
            handleSizeChange(val) {
                this.pageSize = val;
                this.getAllShop();
            },
            // 当前是第几页改变
            handleCurrentChange(val) {
                this.pageNum = val;
                this.getAllShop();
            },
            // 查询表单提交
            searchBtn() {
                this.pageNum = 1;
                this.getAllShop();
            },
            // 获得表格数据
            getAllShop() {
                this.$http.post('cs/shop/queryAll?pageSize=' + this.pageSize + '&pageNum=' + this.pageNum, this.searchFrom)
                    .then(res => {
                        this.tableData = res.data.t.list;
                        this.total = res.data.t.total
                    })
            },
            // 添加表单提交
            addShopBtn() {
                this.$refs["addShop"].validate((valid) => {
                    if (valid){
                        this.$http.post("cs/shop/insert", this.shop).then(res => {
                            console.log(res.data.t);
                            if (Number(res.data.resultCode) === 2000) {
                                this.$message({type: 'success', message: '恭喜你，添加成功'});
                                this.dialogAdd = false;
                                this.getAllShop();
                            } else {
                                this.$message({type: 'error', message: '系统异常'});
                            }
                        })
                    }
                })
            },

            // 编辑表单提交
            updateShopBtn(){
                this.$refs["updateShop"].validate((valid) => {
                    if (valid){
                        this.$http.put("cs/shop/update", this.shop).then(res => {
                            console.log(res.data.t);
                            if (Number(res.data.resultCode) === 2000) {
                                this.$message({type: 'success', message: '恭喜你，修改成功'});
                                this.dialogUpdate = false;
                                this.getAllShop();
                            } else {
                                this.$message({type: 'error', message: '系统异常'});
                            }
                        })
                    }
                })
            },

            // 删除
            handleDelete(index, row) {
                this.$confirm('此操作将永久删除该门店, 是否继续?', '提示', {
                    confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning', center: true
                }).then(() => {
                    this.$http.delete("cs/shop/delete/" + row.id).then(res => {
                        if (Number(res.data.resultCode) === 2000) {
                            this.$message({type: 'success', message: '删除成功!'});
                            this.getAllShop();
                        } else {
                            this.$message({type: 'error', message: '系统异常'});
                        }
                    })
                }).catch(() => {
                    this.$message({type: 'info', message: '已取消删除'});
                });
            },

            // 选择省之后获得所有市
            getCity(item) {
                this.shop.cityId = '';
                this.shop.areaId = '';
                this.$http.get("cs/baseArea/queryCity?id=" + item).then(res => {
                    this.city = res.data.t;
                })
            },
            // 选择省之后获得所有区县
            async getArea(item) {
                this.shop.areaId = '';
                this.$http.get("cs/baseArea/queryArea?id=" + item).then(res => {
                    this.area = res.data.t;
                })
            }
        },
        mounted() {
            this.getAllShop();
            // 获得所有 省
            this.$http.get("cs/baseArea/queryProvince").then(res => {
                this.province = res.data.t;
            })
        }
    }
</script>

<style>

</style>